﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel.Design;
using OpenComposite.EII;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.IO;

namespace OpenComposite.Repository.Tracking
{
	internal sealed class TrackingWorkflowLoader : WorkflowDesignerLoader
	{
		#region Initialization

		internal TrackingWorkflowLoader()
		{
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		public override string FileName
		{
			get { return string.Empty; }
		}

		#endregion Properties

		#region Methods

		public override TextReader GetFileReader(string filePath)
		{
			return new StreamReader(filePath);
		}

		public override TextWriter GetFileWriter(string filePath)
		{
			return new StreamWriter(filePath);
		}

		#endregion Methods

		#endregion Public Members

		#region Internal Members

		#region Properties

		internal CompositeActivity RootActivity
		{
			get { return workflowDefinitionValue as CompositeActivity; }
		}

		internal Activity WorkflowDefinition
		{
			get { return workflowDefinitionValue; }
			set { this.workflowDefinitionValue = value; }
		}

		#endregion Properties

		#endregion Internal Members

		#region Protected Members

		#region Methods

		protected override void PerformFlush(IDesignerSerializationManager manager)
		{
		}

		//Load the workflow definition from WorkflowMarkup
		protected override void PerformLoad(IDesignerSerializationManager serializationManager)
		{
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			Activity activity = this.workflowDefinitionValue;

			//Add the rootactivity the designer
			if ( activity != null && designerHost != null ) {
				Helpers.AddObjectGraphToDesignerHost(designerHost, activity);
				SetBaseComponentClassName(activity.Name);
			}
		}

		#endregion Methods

		#endregion Protected Members

		#region Private Members

		#region Fields

		private Activity workflowDefinitionValue = null;

		#endregion Fields

		#endregion Private Members
	}
}
